#!/usr/bin/env bash
set -euo pipefail

declare -g base prog basedir
base="$(realpath -e "${BASH_SOURCE[0]}")"
prog="$(basename "$base")"
basedir="$(dirname "$base")"
rootdir="$(dirname "$basedir")"

function usage() {
  cat << EOF
Usage: $prog [opt...]

Generate the SchemaStore catalog.

Options:
  -h         Display usage information
  -c CONFIG  Lua table to configure schemastore.generate
EOF
}

function main() {
  local opt OPTARG
  local -i OPTIND
  local config="{}"
  while getopts "hc:" opt "$@"; do
    case "$opt" in
    h)
      usage
      return 0
      ;;
    c)
      config="$OPTARG"
      ;;
    \?)
      return 1
      ;;
    esac
  done
  shift $((OPTIND - 1))

  local baseconfig=""
  local outfile="${1:-}"
  if [[ -n "$outfile" ]]; then
    baseconfig+="$(printf "out = [[%s]]," "$outfile")"
  fi
  baseconfig="{ $baseconfig }"

  nvim --headless -u NORC --noplugin\
    --cmd "set rtp+=$rootdir" \
    -c "$(printf "
      lua local ok, res = pcall(function()
        require'schemastore.generate'.setup(vim.tbl_extend('force', %s, %s)).run()
      end)
      if not ok then
        print(res)
        vim.cmd('cquit')
      end
    " "$baseconfig" "$config")"\
    -c ':q' 2>&1 | tr -d '\r'
}

main "$@"
